import { getChartList, setChart, getIdChart, updateChart, deleteChart } from '@/api/charts'
const charts = {
  namespaced: true,
  state: {
    chartsForm: {},
    chartList: []
  },
  
  mutations: {
    SET_CHARTS_LIST: (state, list) => {
      state.chartList = list.map(item=>{
        return {
          ...item,
          dataMapping: JSON.parse(item.dataMapping), 
          display: JSON.parse(item.display), 
          position: JSON.parse(item.position)
        }
      })
    },
    SET_CHARTS_FORM: (state, form) => {
      state.chartsForm = form
    },
    SET_MOVE: (state, {list, i}) => {
      state.chartList[i].position = list
    },
    SET_RESIZE: (state, {list, i}) => {
      state.chartList[i].position = list
    }
  },
  
  actions: {
    delete_chart ({commit}, id) {
      return new Promise((resolve, reject) => {
        deleteChart({...id})
          .then(res => {
            if (res.code ==0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    get_edit_chart ({commit}, id) {
      return new Promise((resolve, reject) => {
        getIdChart({...id})
          .then(res => {
            if (res.code ==0) {
              commit('SET_CHARTS_FORM', res.data)
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    get_chartList ({commit},id) {
      return new Promise((resolve, reject) => {
        getChartList({...id})
          .then(res => {
            if (res.code ==0) {
              commit('SET_CHARTS_LIST', res.data)
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    update_chart ({commit},form) {
      return new Promise((resolve, reject) => {
        updateChart({...form})
          .then(res => {
            if (res.code ==0) {
              commit('SET_CHARTS_FORM', res.data)
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    set_chart ({commit},id) {
      return new Promise((resolve, reject) => {
        setChart({...id})
          .then(res => {
            if (res.code == 0) {
              commit('SET_CHARTS_FORM', res.data)
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    }
  },

  getters: {

  }
}

export default charts